package com.cf.mvp.sample.mvp.view;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.cf.mvp.base.BaseActivity;
import com.cf.mvp.mvp.annotation.FunPresenter;
import com.cf.mvp.mvp.annotation.Presenter;
import com.cf.mvp.sample.R;
import com.cf.mvp.sample.mvp.contract.MainContract;
import com.cf.mvp.sample.mvp.contract.NetworkContract;
import com.cf.mvp.sample.mvp.contract.RegisterContract;
import com.cf.mvp.sample.mvp.presenter.MainPresenter;
import com.cf.mvp.sample.mvp.presenter.NetworkPresenter;
import com.cf.mvp.sample.mvp.presenter.RegisterPresenter;
import com.cf.mvp.tool.LogUtils;


/**
 * @author yangji
 */
@Presenter(MainPresenter.class)
public class MainActivity extends BaseActivity<MainContract.Presenter> implements MainContract.View, NetworkContract.View, RegisterContract.View {


    @FunPresenter(NetworkPresenter.class)
    private NetworkContract.Presenter presenter;

    @FunPresenter(RegisterPresenter.class)
    private RegisterContract.Presenter registerPresenter;

    private TextView tvUserName, tvUserPwd, tvNetwork;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvUserName = findViewById(R.id.tvUserName);
        tvUserPwd = findViewById(R.id.tvUserPwd);
        tvNetwork = findViewById(R.id.tvNetwork);
    }


    @Override
    public void loginSuccess() {
        runOnUiThread(() -> Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void loginFailed() {
        runOnUiThread(() -> Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show());
    }

    public void btnLogin(View view) {
        String userName = tvUserName.getText().toString();
        String pwd = tvUserPwd.getText().toString();
        getPresenter().login(userName, pwd);
    }


    public void btnRegister(View view) {
        registerPresenter.requestRegister();
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.requestNetwork();
    }

    @Override
    public void onNetworkSignal(Long signal) {
        tvNetwork.setText(String.valueOf(signal));
        LogUtils.d("网络监听", String.valueOf(signal));
    }

    @Override
    public void registerSuccess() {
        Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
    }

    public void btnStop(View view) {
        presenter.requestNetwork();
    }

    public void btnThread(View view) {
        registerPresenter.requestThread();
    }

}
